<!DOCTYPE HTML>
<html lang="cn" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <%include("/common/header.html",{title:''}){}%>
    <title>商品列表</title>
    <link rel="stylesheet" href="css/formSelects-v4.css" />
</head>
<body>

<div class="x-nav" style="margin: 20px auto auto 15px;">
      <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
        <a href="javascript:;">首页</a><span lay-separator="">/</span>
        <a href="javascript:;">商品管理</a><span lay-separator="">/</span>
        <a><cite>商品列表</cite></a>
      </span>
    <a class="layui-btn layui-btn-primary layui-btn-small" style="margin: -5px 14px 0px 0px;float:right;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh-3"></i>
    </a>
</div>



<div class="layui-form layui-card-header layuiadmin-card-header-auto">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">发布时间</label>
            <div class="layui-input-block">
                <input type="text" name="goodsName" placeholder="请输入发布时间..."
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">类别</label>
            <div class="layui-input-block">
                <select type="text" name="classify" id="f_classify11" autocomplete="off" class="layui-select">
                    <option></option>
                    <option>品牌A</option>
                    <option>品牌B</option>
                    <option>品牌C</option>
                </select>
            </div>
        </div>


        <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
        </div>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
            <script type="text/html" id="toolbarDemo">
                <button class="layui-btn layuiadmin-btn-useradmin" lay-event="add">上架商品</button>
                <!--<button class="layui-btn layuiadmin-btn-useradmin" lay-event="combination">组合商品</button>-->
            </script>
            <script type="text/html" id="imgTpl">
                <img style="display: inline-block; width: 50%; height: 100%;" src={{ d.avatar }}>
            </script>
            <script type="text/html" id="table-useradmin-webuser">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>
<!-- 单个商品弹出页面 -->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin"
     id="layuiadmin-form-useradmin"
     style="padding: 15px;display: none">
    <form action class="layui-form" lay-filter="component-form-element" id="myform">

        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">名称:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="goodName" lay-verify="required" value=""  class="layui-input" style="width: 257%"
                       placeholder="请输入商品名称">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">价格:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="number" name="price" lay-verify="required" value=""  class="layui-input" style="width: 257%"
                       placeholder="请输入商品价格">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">介绍:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="introduce" lay-verify="required" value=""  class="layui-input" style="width: 257%"
                       placeholder="请输入介绍">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">反佣:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="commission" lay-verify="required" value=""  class="layui-input" style="width: 257%"
                       placeholder="请输入奖励金额">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">复选:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <select name="ckbox">
                    <option value="0">是</option>
                    <option value="1" selected="">否</option>
                </select>
            </div>
        </div>




        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="component-form-element">立即提交</button>

            </div>
        </div>
    </form>
</div>


<!-- 组合商品弹出页面 -->
<div class="layui-form" lay-filter="layuiadmin-form-useradmins"
     id="layuiadmin-form-useradmins"
     style="padding: 15px;display: none">
    <form action class="layui-form" lay-filter="component-form-elements" id="myforms">
        <input type="hidden" name="id" id="id">
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">名称:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="goodName" lay-verify="required" lay-filter="goodName" value=""  class="layui-input" style="width: 257%"
                       placeholder="请输入商品名称">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">价格:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="number" name="price" lay-verify="required" lay-filter="price" value=""  class="layui-input" style="width: 257%"
                       placeholder="请输入商品价格">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">介绍:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="introduce" lay-verify="required" lay-filter="introduce" value=""  class="layui-input" style="width: 257%"
                       placeholder="请输入介绍">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">奖励佣金:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="commission" lay-verify="required" lay-filter="commission" value=""  class="layui-input" style="width: 257%"
                       placeholder="请输入奖励金额">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">上架时间:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="createTime" lay-verify="required" lay-filter="createTime" value=""  class="layui-input" style="width: 257%" readonly="readonly"
                       placeholder="请输入上架时间">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">复选:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <select name="ckbox">
                    <option value="0">是</option>
                    <option value="1">否</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="component-form-element">立即提交</button>

            </div>
        </div>
    </form>
</div>

<%include("/common/foot.html"){}%>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
<script src="js/formSelects-v4.js" type="text/javascript" charset="utf-8"></script>
<script>

    layui.use(['index', 'table', 'form', 'laydate', 'layedit', 'AjaxCommon', 'AliangLayui'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var upload = layui.upload;
        var layedit = layui.layedit;
        var AliangLayui = layui.AliangLayui;
        var index;


        //富文本编辑器
        layedit = layui.layedit;

        var title = "商品信息";
        var size = ['100%', '100%']
        var findAll_url = 'goods/list/find.jhtml';
        var update_url = 'goods/list/update.jhtml';
        var delete_url = 'goods/list/delete.jhtml';

        var cols = [ //表头
            {checkbox: true, fixed: true},
            {field: 'goodName',  title: '名称'},
            {field: 'price',  title: '价格'},
            {field: 'introduce',  title: '介绍'},
            {field:'commission',title:'奖励佣金'},
            {field: 'createTime',  title: '上架时间'},
            {title: '操作',  toolbar: '#table-useradmin-webuser', fixed: 'right'}
        ];


        AliangLayui.createTable(
            "#LAY-user-manage",
            "listReload",
            "#toolbarDemo",
            title,
            findAll_url,
            true,
            [10, 15, 20, 25],
            ['filter', 'print', 'exports'],
            cols,
            null
        );
        // table.render({
        //     elem: '#LAY-user-manage',
        //     id: 'listReload',
        //     toolbar: '#toolbarDemo',
        //     title: title,
        //     url: findAll_url,
        //     page: true, //开启分页
        //     limits: [10, 20, 30, 50], //控制多少行一页（默认五条一页）
        //     defaultToolbar: ['filter', 'print', 'exports'],
        //     cols: [cols],
        //     done() {
        //         //alert() 数据渲染完的方法
        //     }
        // });

        //自定义验证规则
        form.verify({
            required: [/[\S]+/, "必填项不能为空"],
            image: function(value){
                if(value==""){
                    return '您还未上传图片信息';
                }
            }

        });
        var totalArray = new Array();
        //初始化修改页面
        function loadData(data) {
            var ret = {
                "id": data.id,
                "goodName": data.goodName,
                "price":data.price,
                "introduce":data.introduce,
                "commission":data.commission,
                "createTime":data.createTime,
                "ckbox":data.ckbox
            }

            return ret;
        }

        laydate.render({
            elem: '#create' //指定元素
        });
        //////////////////////////////////////以上是修改每个页面修改的地方
        //弹出修改页面调用初始化数据方法
        function initUpdate(data) {
            layer.open({
                title: title,
                area: size,
                type: 1,
                content: $('#layuiadmin-form-useradmin'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                btn: ['关闭'], //可以无限个按钮
                cancel: function (index, layero) {//取消事件
                    location.replace(location.href);　//layer.open关闭刷新
                },
            });




        }


        function initUpdates(data) {
            layer.open({
                title: title,
                area: size,
                type: 1,
                content: $('#layuiadmin-form-useradmins'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                btn: ['关闭'], //可以无限个按钮
                cancel: function (index, layero) {//取消事件
                    location.replace(location.href);　//layer.open关闭刷新
                },
            });


            layui.form.val("component-form-elements", loadData(data));

        }




        //修改页面的内容提交到后台处理
        form.on('submit(component-form-element)', function (data) {
            data.field.content = layedit.getContent(index);
            postUpdate(data);
            return false;
        });
        //头工具栏事件
        table.on('toolbar(LAY-user-manage)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    initUpdate(0);
                    break;
                case 'combination':
                    initUpdates(0);
                    break;
                case 'export':
                    layer.msg("导出");
                    break;
            }
        });

        //点击每行右工具栏“操作”里面按钮效果
        table.on('tool(LAY-user-manage)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                layer.msg('ID：' + data.id + ' 的查看操作');
            } else if (obj.event === 'edit') {
                    initUpdates(data);
            } else if (obj.event === 'del') {
                layer.confirm('删除以后数据将不能恢复，确定删除吗？', function (index) {
                    //alert(data.id);
                    postDelete(data.id);
                });
            }
        });


        //监听搜索按钮效果
        form.on('submit(LAY-user-front-search)', function (data) {
            var field = data.field;
            table.reload('listReload', {
                where: {
                    name: field.name,
                    price: field.price,
                    pay: field.pay
                },
                page: {
                    curr: 1
                }
            });
        });

        //ajax提交删除内容
        window.postDelete = function (id) {
            $.ajax({
                url: delete_url,
                type: "POST",
                data: {
                    "id": id
                },
                dataType: "json",
                success: function (ret) {
                    if (ret == null) {
                        layer.msg("删除失败", {
                            icon: 5
                        });
                    } else {
                        layer.msg("删除成功", {
                            icon: 6
                        });
                        layer.closeAll();
                        table.reload('listReload');
                    }
                    return ret;
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var estr = "Page:" + window.location.pathname + ",Function: postDelete()";
                    layer.open({
                        title: "错误信息",
                        area: ['100%', '100%'],
                        type: 2,
                        content: "base/error.jhtml?info=" + estr, //
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        }

        //ajax提交修改内容
        window.postUpdate = function (data) {
            var formData = new FormData();
            formData.append("data", JSON.stringify(data.field));
            $.ajax({
                url: update_url,
                type: "POST",
                //traditional:true,
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data.code == 0) {
                        layer.closeAll();
                        layer.msg(data.msg, {time: 600, icon: 1}, function () {
                            //table.reload('listReload');

                        });
                        location.replace(location.href);
                    } else {
                        layer.msg(data.msg, {icon: 6});
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var estr = "Page:" + window.location.pathname + ",Function:postUpdate()";
                    layer.open({
                        title: "错误信息",
                        type: 2,
                        content: "base/error.jhtml?info=" + estr,
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        }
    });

</script>
</body>

</html>